package com.wedimob.boxinternetprotection.dummy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.wedimob.boxinternetprotection.freebox.Freebox;
import com.wedimob.boxinternetprotection.freebox.FreeboxClient;
import com.wedimob.boxinternetprotection.freebox.Refresher;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 * TODO: Replace all uses of this class before publishing your app.
 */
public class FreeboxContent {

    private static String APP_TAG = FreeboxContent.class.getName();

    public static List<FreeboxClient> CLIENTS = new ArrayList<FreeboxClient>();

    public static Map<String, FreeboxClient> CLIENTS_MAP = new HashMap<String, FreeboxClient>();

    public static Freebox freebox;

    private static class FreeboxObserver implements Observer {

        private Context ctx;
        private BaseAdapter adapter;
        private Freebox freebox;
        private String pass;

        public FreeboxObserver(Context ctx, BaseAdapter adapter, Freebox freebox, String pass) {
            super();
            this.ctx = ctx;
            this.adapter = adapter;
            this.freebox = freebox;
            this.pass = pass;
        }

        @Override
        public void update(Observable o, Object arg) {
            (new RefreshFreebox(ctx, adapter, freebox, pass)).execute();
        }
    }

    private static FreeboxObserver freeboxObserver;
    private static Refresher refresher;

    public static void setFreebox(Context ctx, BaseAdapter adapter, Freebox freebox, String pass) {

        freeboxObserver = new FreeboxObserver(ctx, adapter, freebox, pass);
        freebox.addObserver(freeboxObserver);
        refresher = new Refresher(freebox);
        refresher.setAutoRefresh(5);

        // first launch for connecting freebox
        (new RefreshFreebox(ctx, adapter, freebox, pass)).execute();
    }

    private static class RefreshFreebox extends AsyncTask<String, Void, Object> {

        private BaseAdapter adapter;
        private Freebox freebox;
        private Exception exception;
        private String pass;
        private Context ctx;

        public RefreshFreebox(Context ctx, BaseAdapter adapter, Freebox freebox, String pass) {
            this.adapter = adapter;
            this.freebox = freebox;
            this.pass = pass;
            this.ctx = ctx;
        }

        public Exception getException() {
            return exception;
        }

        protected Object doInBackground(String... task) {
            try {
                if (freebox != null) {
                    CLIENTS.clear();
                    CLIENTS_MAP.clear();

                    if (!freebox.isConnected()) {
                        freebox.connect(pass);
                    }

                    List<FreeboxClient> clients = freebox.getClients();
                    CLIENTS.addAll(clients);
                    for (FreeboxClient client : clients)
                        CLIENTS_MAP.put(client.getMac(), client);

                    return freebox;
                }
            } catch (Exception e) {
                this.exception = e;
            }
            return null;
        }

        protected void onPostExecute(Object result) {
            if (result != null) {
                Log.i(APP_TAG, "Refresh FreeboxContent");
                adapter.notifyDataSetChanged();
            }
            else {
                if (freebox.isConnected()) {
                    freebox.disconnect();
                }
                Toast.makeText(ctx, exception.getMessage(), Toast.LENGTH_SHORT);
            }
        }
    }

    static public void cancelRefresh() {
        if (refresher != null) {
            refresher.cancelAutoRefresh();
        }
    }

    public static void pauseRefresh() {
        if (refresher != null)
            refresher.cancelAutoRefresh();
    }

    public static void resumeRefresh() {
        if (refresher != null)
            refresher.resumeAutoRefresh();
    }
}
